/*
 * Copyright 2006-2023 ahtelit.com, All Rights Reserved.
 */

package com.telit.android.controller;

import com.alibaba.excel.EasyExcel;
import com.telit.android.service.ExportService;
import com.telit.common.constant.Constant;
import com.telit.firecommand.model.easyExcel.FirePointExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;


/**
 * 导出API（firecommand服务中引入easyexcel会报poi版本问题）
 *
 * @author yuezhuang
 * @since 2023-03-10
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "导出API", value = "导出API", produces = MediaType.APPLICATION_JSON_VALUE)
public class ExportController {

    private final ExportService exportService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "districtCode", value = "所属行政区划编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "fireLevel", value = "火情等级", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "resource", value = "火情来源", paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/export-history-fire-list", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "导出历史火情列表", httpMethod = Constant.REQUEST_METHOD_GET)
    public void exportHistoryFireList(@RequestParam(required = false) String startTime,
                                      @RequestParam(required = false) String endTime,
                                      @RequestParam(required = false) String districtCode,
                                      @RequestParam(required = false) String fireLevel,
                                      @RequestParam(required = false) String resource,
                                      HttpServletResponse response) throws IOException {
        exportService.exportHistoryFireList(startTime, endTime, districtCode, fireLevel, resource, response);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "districtCode", value = "所属行政区划编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "resource", value = "火情来源", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "火情状态", paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/export-fire-list", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "导出火情列表", httpMethod = Constant.REQUEST_METHOD_GET)
    public void exportFireList(@RequestParam(required = false) String status,
                               @RequestParam(required = false) String startTime,
                               @RequestParam(required = false) String endTime,
                               @RequestParam(required = false) String districtCode,
                               @RequestParam(required = false) String resource,
                               HttpServletResponse response) throws IOException {
        exportService.exportFireList(status, startTime, endTime, districtCode, resource, response);
    }

    @GetMapping(value = "/template/fire-point", produces = "application/json;charset=UTF-8")
    @ApiOperation("下载火烧迹地模板")
    public void template(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("经纬度集合文件模板", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        // 设置文件名
        response.setHeader("filename", fileName + ".xlsx");
        // 设置服务器headers白名单，可以让客户端进行访问操作的属性
        response.setHeader("Access-Control-Expose-Headers", "filename");
        EasyExcel.write(response.getOutputStream(), FirePointExcel.class).sheet(fileName).doWrite(
                new ArrayList<FirePointExcel>());
    }

}
